package io.admin.modules.sys.controller;

import io.admin.common.annotation.LogOperation;
import io.admin.common.constant.Constant;
import io.admin.common.page.PageData;
import io.admin.common.utils.ExcelUtils;
import io.admin.common.utils.Result;
import io.admin.common.validator.AssertUtils;
import io.admin.common.validator.ValidatorUtils;
import io.admin.common.validator.group.AddGroup;
import io.admin.common.validator.group.DefaultGroup;
import io.admin.common.validator.group.UpdateGroup;
import io.admin.modules.sys.dto.UserEquipmentDTO;
import io.admin.modules.sys.excel.UserEquipmentExcel;
import io.admin.modules.sys.service.UserEquipmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 用户设备管理
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2024-07-04
 */
@RestController
@RequestMapping("sysapp/userequipment")
@Api(tags="用户设备管理")
public class UserEquipmentController {
    @Autowired
    private UserEquipmentService userEquipmentService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String"),
        @ApiImplicitParam(name = "username", value = "用户昵称", paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "mobile", value = "用户账号", paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "bumber", value = "设备号", paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "state", value = "状态", paramType = "query", dataType = "String")
    })
    /*@RequiresPermissions("sys:userequipment:page")*/
    public Result<PageData<UserEquipmentDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<UserEquipmentDTO> page = userEquipmentService.page(params);

        return new Result<PageData<UserEquipmentDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
   /* @RequiresPermissions("sys:userequipment:info")*/
    public Result<UserEquipmentDTO> get(@PathVariable("id") Long id){
        UserEquipmentDTO data = userEquipmentService.get(id);

        return new Result<UserEquipmentDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
   /* @RequiresPermissions("sys:userequipment:save")*/
    public Result save(@RequestBody UserEquipmentDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        dto.setSource(1);
        userEquipmentService.save(dto);
        return new Result();
    }

    @PutMapping("/update")
    @ApiOperation("修改")
    @LogOperation("修改")
   /* @RequiresPermissions("sys:userequipment:update")*/
    public Result update(@RequestBody UserEquipmentDTO dto){
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        if(null!=dto.getId()){
            userEquipmentService.update(dto);
        }
        if(null!=dto.getIds()){
            String[] ids = dto.getIds().split(",");
            for (String id:ids){
                UserEquipmentDTO dtos =new UserEquipmentDTO();
                dtos.setState(dto.getState());
                dtos.setId(Long.valueOf(id));
                dtos.setUpdateDate(new Date());
                userEquipmentService.update(dtos);
            }

        }
        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
  /*  @RequiresPermissions("sys:userequipment:delete")*/
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        userEquipmentService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
   /* @RequiresPermissions("sys:userequipment:export")*/
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<UserEquipmentDTO> list = userEquipmentService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, "用户设备管理", list, UserEquipmentExcel.class);
    }

}